package edu.ccut.saturn.basecomponent.database.impl;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

import edu.ccut.saturn.basecomponent.database.ISaturnSQL;
import edu.ccut.saturn.basecomponent.database.ISaturnSQLHandler;
import edu.ccut.saturn.basecomponent.database.preparedstatement.PreparedStatementSetUtils;
import edu.ccut.saturn.basecomponent.database.util.FieldAndValueVO;
import edu.ccut.saturn.basecomponent.database.util.SaturnDataUtil;
import edu.ccut.saturn.basecomponent.database.valuetype.ValueTypeSetUtils;
import edu.ccut.saturn.component.IDataInfo;
import edu.ccut.saturn.component.IDataItem;
import edu.ccut.saturn.component.ILogger;
import edu.ccut.saturn.component.SaturnLoggerManager;

public abstract class AbstractSQLHandler implements ISaturnSQLHandler {
	private static final String LOGGER_KEY_BUNDLE_RUNTIME = SaturnLoggerManager.LOGGER_KEY_COMPONENT_RUNTIME;
	protected static ILogger logger = SaturnLoggerManager.getInstance()
	.getSaturnLogger(LOGGER_KEY_BUNDLE_RUNTIME);
	
//	public ILogger getLogger() {
//		return logger;
//	} 
	public void info(String message, Throwable throwable) {
		logger.info(message, throwable);
	}

	public void info(String message) {
		logger.info(message);
	}

	public void debug(String message, Throwable throwable) {
		logger.debug(message, throwable);
	}

	public void debug(String message) {
		logger.debug(message);
	}

	public void error(String message, Throwable throwable) {
		logger.error(message, throwable);
	}

	public void error(String message) {
		logger.error(message);
	}

	public void warn(String message, Throwable throwable) {
		logger.warn(message, throwable);
	}

	public void warn(String message) {
		logger.warn(message);
	}

	public void fatal(String message, Throwable throwable) {
		logger.fatal(message, throwable);
	}

	public void fatal(String message) {
		logger.fatal(message);
	}

	protected int setConditionPrepareStatement(ISaturnSQL saturnSQL, int j, PreparedStatement ps)
			throws SQLException {
		int i = j;
		List<FieldAndValueVO> conditionList = saturnSQL.getConditionList();
		if (conditionList != null){
			String fullKey = saturnSQL.getSaturnDataFullKey();
			IDataInfo dataInfo = SaturnDataUtil.getDataInfo(fullKey);
		
			for (FieldAndValueVO vo : conditionList) {
				String value = vo.getValue();
				String field = vo.getField();
		
				IDataItem dataItem = dataInfo.getDataItem(field);
				String javaType = null;
				String sqlType = null;
				if(dataItem != null){
					javaType = dataItem.getJavaType();
					sqlType = dataItem.getType();
				}else{
					javaType = "java.lang.String"; 
					sqlType = "VARCHAR"; 
				}
				Object changedValue = ValueTypeSetUtils.getValueType(javaType, value);
				sqlType = getSpecialSqlKey(sqlType);
				if ("NUMBER".equalsIgnoreCase(sqlType)){
					sqlType = javaType;						
				}
				debug("parameter " + i + " = " + changedValue.toString());
				PreparedStatementSetUtils.setValue(sqlType, ps, i++, changedValue);
			}
			saturnSQL.setConditionList(null);
		}
		return i;
	}
	
	protected String getSpecialSqlKey(String sqlType) {
		return sqlType;
	}
}
